
package com.scalar.chiptrack.operations.process.actions;


import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.FabProcessSplitDAO;
import com.scalar.chiptrack.operations.process.dao.SortProcessSplitDAO;
import com.scalar.chiptrack.operations.process.dao.TestProcessSplitDAO;
import com.scalar.chiptrack.users.User;


public final class TestProcessSplitAction extends ChipTrackAction {

	public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception 
    {
		FabProcessSplitDAO  fabProcessSplitDao     = new FabProcessSplitDAO();
		SortProcessSplitDAO sortProcessSplitDao    = new SortProcessSplitDAO();
		TestProcessSplitDAO testProcessSplitDao    = new TestProcessSplitDAO();

		HttpSession ses = request.getSession(true);
		User user = (User) ses.getAttribute (SessionKeys.USER_KEY);
		if (user == null)
		{
			return (mapping.findForward ("login"));
		}

		String productId = (String)ses.getAttribute("productId");
		String [] assyProducts = (String [])ses.getAttribute("assemblyProducts");
		String assyProduct = "";
		String binSetUp    = "1";
        String [] fabProducts = (String [])ses.getAttribute("fabProduct");

        HashMap assyHash = (HashMap)ses.getAttribute("assemblyHash");
		ArrayList testNameList = (ArrayList)ses.getAttribute("testNames");
		ArrayList assemblyTrackList = (ArrayList)ses.getAttribute("assemblyTrackList");
		String checkTest = testProcessSplitDao.getTest((String)assemblyTrackList.get(0));
		if( !isTokenValid(request) )
		{
			System.out.println(" into reset in test ");
			request.setAttribute ("baseProductID", productId);
			return (mapping.findForward("testsuccess")); 
		}	
		if(checkTest.equals("exists"))
		{
			//System.out.println("Test Already Exists ..");
			//sortProcessSplitDao.delete(pageId);
		}
		else
		{					
			for( int i = 0 ; i < assemblyTrackList.size() ; i++ )
			{
				assyProduct =	(String)assyHash.get( assemblyTrackList.get(i) ); 
				String assyFirstChar = assyProduct.charAt(0)+"";

				String newTestsCount = (String)request.getParameter( "assy" + assyFirstChar+assemblyTrackList.get(i) + "addMoreCount" );
				int testsCount=new Integer(newTestsCount).intValue();
				if( !assyProduct.equals("") )	
				{
					for( int k=0; k < testNameList.size(); k++ )
					{
						String values = request.getParameter("assy"+assyFirstChar+assemblyTrackList.get(i)+"check"+k);
						String seq    = request.getParameter("assy"+assyFirstChar+assemblyTrackList.get(i)+"seq"+k);				
						if(values != null && seq !=null)
						{
					    	testProcessSplitDao.save((String)assemblyTrackList.get(i),(String)testNameList.get(k),seq,binSetUp,"Incomplete");
						}
					}
				    
				    for( int l=1; l <= testsCount; l++ )
				    {
						String checkValue=(String)request.getParameter("assy"+assyFirstChar+assemblyTrackList.get(i)+"addMorecheck"+l);
						//System.out.println("checkValue "+checkValue);
						String newTestName=(String)request.getParameter("assy"+assyFirstChar+assemblyTrackList.get(i)+"addMorename"+l);
						//System.out.println("newTestName "+newTestName);
						String newSeq =(String)request.getParameter("assy"+assyFirstChar+assemblyTrackList.get(i)+"addMoreseq"+l);
						//System.out.println("newSeq "+newSeq);
						if(checkValue != null && newTestName !=null && newSeq !=null && newTestName.trim().length() > 0 )
					 	{
							testProcessSplitDao.save((String)assemblyTrackList.get(i),newTestName,newSeq,binSetUp,"Incomplete");
							testProcessSplitDao.saveTestName(newTestName);
						}	
				 	}
					     		 
					String classStore = request.getParameter("assy"+assyFirstChar+assemblyTrackList.get(i)+"wafer");
					if(classStore.equals("0"))
					{
				    	testProcessSplitDao.saveClassStore((String)assemblyTrackList.get(i),"Incomplete");
					}
					testProcessSplitDao.updateBinSetUp((String)assemblyTrackList.get(i));
				}	
	  		}
		}
		resetToken(request);
	    request.setAttribute ("baseProductID", productId);	    
        return (mapping.findForward("testsuccess")); 
	}
}